<?php require_once(dirname(__FILE__).'/inc/config.inc.php');
$action = isset($action) ? $action : '';
$gourl = 'template.php';
//更换模板
if($action == 'update_theme')
{	
	$theme_inc = PHPMYWIND_INC.'/theme.cache.php';
	$file = PHPMYWIND_TEMP.'/'.$theme;
	$str='';
	$str .= "\$cfg_theme = '".$theme."';\r\n";
	
	$str = '<?php	if(!defined(\'IN_PHPMYWIND\')) exit(\'Request Error!\');'."\r\n\r\n".$str."\r\n".'?>';
	if(file_exists($file)){
		

			if(Writef($theme_inc, $str))
			{
				AjaxMsg('成功更换模板！', $gourl,'icon: 1,time:1000','msg');
				exit();
			}
			else
			{
				AjaxMsg('更换模板失败，可能是由于没有写入权限，因此不能更新配置！', $gourl,'icon: 5,time:2000','msg');
				exit();
			}
			
	}
	else{
		AjaxMsg('更换模板失败，模板文件不存在！', $gourl,'icon: 5,time:2000','msg');
		exit();
	}
}

//删除模板
if($action == 'del_theme')
{	
	if(!ctype_alnum($theme))//检查是否是字母或数字或字母数字的 组合
	{
		AjaxMsg('非法的模板名称！', $gourl,'icon: 00,time:2000','msg');
		exit();
	}
	$file = PHPMYWIND_TEMP.'/'.$theme;
	if(file_exists($file) or empty($theme)){
			if(DeleteAll($theme)){
				AjaxMsg('删除模板成功！', $gourl,'icon: 1,time:1000','msg');
				exit();
			}
			else
			{
				AjaxMsg('删除失败，可能是由于没有删除权限！', $gourl,'icon: 5,time:2000','msg');
				exit();	
			}
	}
	else{
		AjaxMsg('删除模板失败，模板文件不存在！', $gourl,'icon: 5,time:2000','msg');
		exit();
	}
	
	
}


/*
 * 函数说明：删除文件夹及其文件夹下所有文件
 *         
 *限制目录在模板
 */

		function DeleteAll($path) {
			$path= PHPMYWIND_TEMP.'/'.$path;
			$op = dir($path);
			while(false != ($item = $op->read())) {
				if($item == '.' || $item == '..') {
					continue;
				}
				if(is_dir($op->path.'/'.$item)) {
					deleteAll($op->path.'/'.$item);
					rmdir($op->path.'/'.$item);
				} else {
					unlink($op->path.'/'.$item);
				}
			
			}  
		  //删除当前文件夹：
		  if(rmdir($path)) {
			return true;
		  } else {
		  return false;
		  }			
		}


?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0,user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>模板管理</title>
<link href="templates/css/style.css" rel="stylesheet" type="text/css" />
<link href="templates/css/pagination.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" charset="utf-8" src="templates/js/jquery-1.11.2.min.js"></script>
<script src="../plugin/layer/layer.js"></script>
<script>
function CheckTheme(id,check) {
	eval(layer.load(1));
	$.ajax({
		url : 'template.php?action='+check+'_theme&theme='+id,
		type:'post',
		async : false,
		dataType:'text',
		success:function(data){
			eval(data);
		},
		error: function (jqXHR, textStatus, errorThrown) {
		 layer.alert('ajax error');
		}
	});
}
</script>

</head>

<body class="mainbody">

<!--导航栏-->
<div class="location">
  <a href="javascript:history.back(-1);" class="back"><i></i><span>返回上一页</span></a>
  <a href="home.php" class="home"><i></i><span>首页</span></a>
  <i class="arrow"></i>
  <span>模板管理</span>
  <a href="javascript:location.reload();" class="reload">刷新</a>
</div>
<!--/导航栏-->

<!--工具栏-->
<div id="floatHead" class="toolbar-wrap">

</div>
<!--/工具栏-->

<!--列表-->
<div class="table-container">
  <!--文字列表-->
  
  <!--/文字列表-->

  <!--图片列表-->
  
  <div class="imglist">
    <ul>
  <?php  

		$themes= get_theme_all(PHPMYWIND_TEMP) ;
		// 启用的主题放在第一
		if(isset($themes[$cfg_theme])) {
			$tmp = array();
			$tmp[$cfg_theme] = $themes[$cfg_theme];
			unset($themes[$cfg_theme]);
			$themes = $tmp + $themes;
		}
		foreach($themes as $file) {
			if($file['theme']==$cfg_theme) $check ='已启用';
			else $check ='<a href="javascript:void(0)" onclick="CheckTheme(\''.$file['theme'].'\',\'update\')">启用</a>'
		?>
      <li>
        <div class="details">
          <div class="pic"><img src="../templates/<?php echo $file['theme'];?>/preview.jpg"/>
		  </div><i class="absbg"></i>
          <h1><span><a href=""><?php echo $file['name'];?></a></span></h1>
          <div class="remark">
		  	<p>作者：<?php echo $file['author'];?></p>
			<p title="更新时间 ：<?php echo $file['update'];?>">版本：<?php echo $file['version'];?></p>
			<p>更新：<?php echo $file['update'];?></p>
			<p>简介：<?php echo $file['description'];?></p>
          </div>

          <div class="foot">
            <span ><?php echo $check;?></span>
			<a href="javascript:void(0)" onclick="CheckTheme('<?php echo $file['theme'];?>','del')" title="删除模板" class="del">删除</a>
            <a href="#" title="预览模板" class="preview">预览</a>
          </div>
        </div>
      </li>
	<?php 
		}
	?>
	<?php if(!isset($themes)) echo ' <div align="center" style="font-size:12px;line-height:30px;color:#666;">暂无模板</div>';
	else echo '<div align="center" style="width:100%;float:left;font-size:12px;line-height:30px;color:#666;">共有'.count($themes).'个模板</div>'
	;?>
    </ul>
  </div>
  
  <!--/图片列表-->
</div>
<!--/列表-->

</body>
</html>
